﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Hugo
{
    public abstract class UIPage : UIOption
    {
        
        public MainWindow MainWindow { get; set;  }
        public Canvas ParentCanvas { get; set; }
        public Canvas PageCanvas { get; set; }
       // public Canvas NavCanvas { get; set; }

        static int nextPageCanvasID = 0;

        public const int ICON_WIDTH_SELECTED = 200;
        public const int ICON_HEIGHT_SELECTED = 200;
        public const int ICON_WIDTH_UNSELECTED = 100;
        public const int ICON_HEIGHT_UNSELECTED = 100;
        public const int ICON_BUFFER = 0;

        public UIPage(String name, BitmapSource iconSource) : base(name,iconSource)
        {
            this.MainWindow = MainWindow.getInstance();
            //this.Name = name;
            this.IconSource = iconSource;
            this.ParentCanvas = this.MainWindow.mainCanvas;
            PageCanvas = new Canvas();
            PageCanvas.Name = name.Replace(" ","") + "_pagecanvas" + (nextPageCanvasID++);
            this.MainWindow.RegisterName(PageCanvas.Name, PageCanvas);  
            ParentCanvas.Children.Add(PageCanvas);
            PageCanvas.Opacity = 0;
        }

        public override void wasSelected() {
            MainWindow.pushPage(this);
        }

        public virtual void setPage()
        {
            DoubleAnimation myDoubleAnimation = AnimationFactory.getFadeInAnimation();

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, PageCanvas.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

            myStoryboard.Begin(this.MainWindow);
        }

        public abstract void respondToKey(KeyEventArgs e);
        public abstract void setCursorPosition(Point p);

        public virtual void unSetPage()
        {
            DoubleAnimation myDoubleAnimation = AnimationFactory.getFadeOutAnimation();

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, PageCanvas.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

            myStoryboard.Begin(this.MainWindow);
        }

        public void addNavigationUIOptions(List<UIOption> options)
        {
            int middle = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2;
            double navIconY = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 2 + ICON_HEIGHT_SELECTED + 50;

            double startX = middle - (((double)options.Count) / 2) * ICON_WIDTH_SELECTED + (options.Count / 2) * ICON_BUFFER;

            int i = 0;
            foreach (UIOption o in options)
            {
                //Point p = getCenterOfRectangleAtIndex(i);
                Rectangle rect = getSymbol(o.IconSource);
                //PageCanvas.Children.Add(rect);
                Canvas.SetLeft(rect, startX + ICON_WIDTH_SELECTED * i + ICON_BUFFER * i); //p.X);
                Canvas.SetTop(rect, navIconY);
                i++;
            }
        }

        public static Rectangle getSymbol(BitmapSource u)
        {
            Rectangle item = new Rectangle();
            item.Height = ICON_HEIGHT_SELECTED;
            item.Width = ICON_WIDTH_SELECTED;
            System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
            myBrush.ImageSource = u;
            item.Fill = myBrush;

            return item;
        }
    }
}
